//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isShowPhone: false,
    logincode: "",
    isShowing: false,
    type:0,
    project_id: "",
    id : "",
    encryptedData: "",
    iv: "",
    date:"",
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '/pages/home2/home'
    })
  },
  onLoad: function (option) {
    var that=this
    wx.setNavigationBarTitle({
      title: '昊信联行客户管理系统',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    this.setData({
      type: option.type,
      project_id: option.project_id,
      id : option.id,
      date: option.date
    })
    if(app.globalData.access_token){
      if (that.data.type == 1) {
        if (app.globalData.isCustomerDistribution) {
          wx.reLaunch({
            url: '/pages/fenpeilist/fenpeilist?project_id=' + that.data.project_id,
          })
        } else {
          wx.reLaunch({
            url: '/pages/list/listCustom?project_id=' + that.data.project_id,
          })
        }
      } else if (that.data.type == 2) {
        wx.reLaunch({
          url: '/pages/newxf/newxf',
        })
      } else if (that.data.type == 3) {
        wx.reLaunch({
          url: '/pages/item/item?id=' + that.data.id,
        })
      } else if (that.data.type == 4) {
        wx.reLaunch({
          url: '/pages/xfdetails/newxf?id=' + that.data.project_id + "&date=" + that.data.date,
        })
      } else {
        if (app.globalData.isMangerHome) {
          wx.reLaunch({
            url: '/pages/home4/home4',
          })
        } else if (app.globalData.isHomeXiaoFei) {
          console.log('home')
          wx.reLaunch({ url: '/pages/home3/home3' })
        } else {
          wx.reLaunch({
            url: '/pages/home2/home2',
          })
        }
    }
    return
    }
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        app.globalData.userInfo = res.userInfo
console.log(11);
        this.setData({
            isShowing:true,
            encryptedData: res.encryptedData,
            iv:res.iv
        })
     
        this.login()
      }
    })

    if (app.globalData.userInfo) {
      this.login();
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          iv: res.iv,
      encryptedData: res.encryptedData
        })
        this.login();
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
            iv: res.iv,
      encryptedData: res.encryptedData
          })
          this.login();
        }
      })
    }
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData,
      hasUserInfo: true
    })
    this.login();
  },
  getPhoneNumber:function(e) {
    var that=this
    //发送服务器解密
    wx.request({
      url: app.globalData.httpurl + 'decryptData',
      method: "POST",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        "iv": e.detail.iv,
        "encryptData": e.detail.encryptedData
      },
      success: function (res) {
        wx.request({
          url: app.globalData.httpurl + 'bind/mobile',
          method: "POST",
          header: {
            "Authorization": "bearer " + app.globalData.access_token
          },
          data: {
            "mobile": res.data.phoneNumber
          },
          success: function (res) {
            if (res.data.code == 1) {
              wx.showToast({
                title: '请等待审核',
              })
              wx.redirectTo({
                url: '/pages/shenhe/shenhe',
              })
              // that.login()
            }else{
              wx.showToast({
                title: res,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  login:function(){
    var that = this
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        that.setData({
          logincode: res.code
        })
        // console.log(this.data.logincode)
        wx.request({
          url: app.globalData.httpurl + 'login',
          method: "POST",
          data: {
            "avatarUrl": app.globalData.userInfo.avatarUrl,
            "city": app.globalData.userInfo.city,
            "nickName": app.globalData.userInfo.nickName,
            "province": app.globalData.userInfo.province,
            "code": that.data.logincode
          },
          success: function (res) {
            // console.log(res.data)
            // console.log(that.data.iv)
            // console.log(that.data.encryptedData)
            if (res.data.status == 1) {
              app.globalData.access_token = res.data.data.access_token;
              console.log(app.globalData.access_token);
              
            

              //获取当前登录是否绑定手机
              wx.request({
                url: app.globalData.httpurl + 'user',
                method: "GET",
                header: {
                  "Authorization": "bearer " + app.globalData.access_token
                },
                data: {

                },
                success: function (res) {
                  console.log('res',res)
                  if(!res.data.data[0].unionid){
                    console.log('进来了')
                  //获取unionid
                  wx.request({
                    url: app.globalData.httpurl + 'decryptData',
                    method: "POST",
                    header: {
                      "Authorization": "bearer " + app.globalData.access_token
                    },
                    data: {
                      "iv": that.data.iv,
                      "encryptData": that.data.encryptedData
                    },
                    success: function (res) {
                      // console.log(res.data)
                      //上传unionid
                      wx.request({
                        url: app.globalData.httpurl + 'users/unionid',
                        method: "POST",
                        header: {
                          "Authorization": "bearer " + app.globalData.access_token
                        },
                        data: {
                          "unionid": res.data.unionId
                        },
                        success: function (res) {
                          // console.log(res.data)
                        }
                      })
                    }
                  })
                  }
                  if (res.data.data[0].mobile == null) {
                    that.setData({
                      isShowing:false,
                      isShowPhone: true
                    })
                  } else {
                    if (res.data.data[0].is_passed == 0){
                      //跳转待审核页面
                      wx.reLaunch({
                        url: '/pages/shenhe/shenhe',
                      })
                    }else{
                      //获取当前登录用户的权限信息
                      wx.request({
                        url: app.globalData.httpurl + 'permission',
                        method: "GET",
                        header: {
                          "Authorization": "bearer " + app.globalData.access_token
                        },
                        data: {

                        },
                        success: function (res) {
                          app.globalData.permission = [];
                          var datas = res.data.data
                          console.log(datas)
                          for (var i = 0; i < datas.length; i++) {
                            app.globalData.permission.push(datas[i].name)
                            if (datas[i].name == 'home_xiaofei') {
                              app.globalData.isHomeXiaoFei = true
                            }
                            if (datas[i].name == 'home_xiaofei'){
                              app.globalData.isHomeNoXiaoFei = true
                            }
                            if (datas[i].name == 'customer_add') {
                              app.globalData.isAddCustomer = true
                            }
                            if (datas[i].name == 'xiaofei_add') {
                              app.globalData.isAddXiaoFei = true
                            }
                            if (datas[i].name == 'customer_manger') {
                              app.globalData.isCustomerManger = true
                            }
                            if (datas[i].name == 'customer_distribution') {
                              app.globalData.isCustomerDistribution = true
                            }
                            if (datas[i].name == 'customer_telephone') {
                              app.globalData.isCustomerTelephone = true
                            }
                            if (datas[i].name == 'distribution') {
                              app.globalData.isCustomDetailsFenPei = true
                            }
                            if (datas[i].name == 'callMobile') {
                              app.globalData.isCustomDetailsBoDa = true
                            }
                            if (datas[i].name == 'manger_home') {
                              app.globalData.isMangerHome = true
                              console.log(222)
                            }
                         
                          }
                          console.log(app.globalData)
                          //权限获取完毕，跳转首页
                          if(that.data.type == 1){
                            if (app.globalData.isCustomerDistribution) {
                              wx.reLaunch({
                                url: '/pages/fenpeilist/fenpeilist?project_id='+that.data.project_id,
                              })
                            } else {
                              wx.reLaunch({
                                url: '/pages/list/listCustom?project_id='+that.data.project_id,
                              })
                            }
                          }else if(that.data.type == 2){
                            wx.reLaunch({
                              url: '/pages/newxf/newxf',
                            })
                          }else if(that.data.type == 3){
                            wx.reLaunch({
                              url: '/pages/item/item?id='+ that.data.id,
                            })
                          }else if(that.data.type == 4){
                            wx.reLaunch({
                              url: '/pages/xfdetails/newxf?id='+ that.data.project_id+"&date="+that.data.date,
                            })
                          }else{
                            if (app.globalData.isMangerHome){
                              wx.reLaunch({
                                url: '/pages/home4/home4',
                              })
                            }else if(app.globalData.isHomeXiaoFei){
                              console.log('home')
                               wx.reLaunch({url:'/pages/home3/home3'}) 
                            }else{
                              wx.reLaunch({
                                url: '/pages/home2/home2',
                              })
                            }
                          }
                        }
                      })
                    }
                  }
                }
              })
            }
          }
        })
        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res)
        }
      }
    })
  }
})
